#include "../include/CMenuRenderer.h"
#include "../include/CSDLManager.h"
#include "../include/CLevelRenderer.h"

#include <SDL/SDL.h>
#include <math.h>

//**************************
//Description : Constructor
//Parameters : None
//Note : Private because a single instance is needed
//**************************
CMenuRenderer::CMenuRenderer()
    : m_iOptionSelected(0)
{
}

//**************************
//Description : Destructor
//Parameters : None
//Note : None
//**************************
CMenuRenderer::~CMenuRenderer()
{
}

//**************************
//Description : Init the instance
//Parameters : None
//Return Value : None
//Note : None
//**************************
void CMenuRenderer::Init()
{
    m_SDLManager = CSDLManager::GetInstancePtr();
    m_LevelRenderer = CLevelRenderer::GetInstancePtr();

    m_BackgroundMenu = m_SDLManager->GetBackgroundMenu();
    m_GameTitle = m_SDLManager->GetTitleSurface();
    m_Menu = m_SDLManager->GetMenuSurfaces();
    m_Highscore = m_SDLManager->GetHighscoreMenuSurfaces();
    m_Numbers = m_SDLManager->GetNumbersSurfaces();

    m_lHighScore = m_LevelRenderer->GetHighScore();
}

//**************************
//Description : DeInit the instance
//Parameters : None
//Return Value : None
//Note : None
//**************************
void CMenuRenderer::DeInit()
{
}

//**************************
//Description : Start the game with the animation (TODO) and launch the menu
//Parameters : None
//Return Value : None
//Note : None
//**************************
void CMenuRenderer::StartMenu()
{
    Menu();
}

//**************************
//Description : Draw the menu
//Parameters : None
//Return Value : None
//Note : None
//**************************
void CMenuRenderer::DrawMenu()
{
    SDL_Rect Dest;
    SDL_Surface * Screen = m_SDLManager->GetScreen();

    // We first fill the entire screen with black
    SDL_FillRect(m_SDLManager->GetScreen(), NULL, 0);

    //we draw the background
    Dest.x =0;
    Dest.y = 0;

    SDL_BlitSurface(m_BackgroundMenu, NULL, Screen, &Dest);

    //we draw the title at the top of the screen an to the center
    Dest.x = (WINDOW_WIDTH / 2) - (m_GameTitle->w / 2);
    Dest.y = 0;

    SDL_BlitSurface(m_GameTitle, NULL, Screen, &Dest);

    Dest.x = 50;

    //we draw the options
    for (int i = 0; i < NUMBER_OPTION_MENU; i++)
    {
        Dest.y = (Sint16)(m_GameTitle->h*1.5 + i * MENU_OPTION_HEIGHT);

        if ( i == m_iOptionSelected )
            SDL_BlitSurface(m_Menu[i*2], NULL, Screen, &Dest);
        else
            SDL_BlitSurface(m_Menu[i*2 + 1], NULL, Screen, &Dest);
    }

    //we draw the highscore
    long lHigh = 0;     //highscore of the player
    int iNum;       //number to draw
    int iPow = 1;       //result of pow

    Dest.y = WINDOW_HEIGHT/2;
    Dest.x = 50 + WINDOW_WIDTH/2;

    SDL_BlitSurface(m_Highscore, NULL, Screen, &Dest);

    lHigh = m_lHighScore;

    //we draw the value of the highscore
    for (int j = 0; j < 9; j++)
    {
        Dest.y = WINDOW_HEIGHT/2 + m_Highscore->h;
        Dest.x = 50 + (WINDOW_WIDTH/2) + j * m_Numbers[j]->w;
	
		for (int a = 0; a < (8-j); a++)
		  iPow *= 10;

        iNum = lHigh / iPow;
        lHigh -= iNum * iPow;

        SDL_BlitSurface(m_Numbers[iNum], NULL, Screen, &Dest);

	iPow = 1;
    }
	

    //We flip the screen
    m_SDLManager->Flip();
}

//**************************
//Description : Manage the menu
//Parameters : None
//Return Value : None
//Note : None
//**************************
void CMenuRenderer::Menu()
{
    bool bDone = false;
    SDL_Event event;

    SDL_EnableKeyRepeat(100, 50);

    while (!bDone)
    {
        while ( SDL_PollEvent(&event) )
        {
            if ( event.type == SDL_QUIT )
                bDone = true;

            if ( event.type == SDL_KEYDOWN )
            {
                if ( event.key.keysym.sym == SDLK_ESCAPE )
                    bDone = true;
                if ( event.key.keysym.sym == SDLK_UP )
                    m_iOptionSelected = ((m_iOptionSelected - 1 + NUMBER_OPTION_MENU) % NUMBER_OPTION_MENU);
                if ( event.key.keysym.sym == SDLK_DOWN )
                    m_iOptionSelected = (m_iOptionSelected + 1) % NUMBER_OPTION_MENU;
                if ( event.key.keysym.sym == SDLK_RETURN )
                {
                    switch (m_iOptionSelected)
                    {
                        //we launch the game from the start with 1 player
                        case 0:
                        {
                            m_LevelRenderer->StartGame(1);

                            break;
                        }
                        //we launch the game from the start with 2 players
                        case 1:
                        {
                            m_LevelRenderer->StartGame(2);

                            break;
                        }
                        //we launch the game from a saved game
                        case 2:
                        {
                            m_LevelRenderer->StartGame( szSaveGameDir.c_str() );

                            break;
                        }
                        //we quit the game
                        case 3:
                        {
                            bDone = true;

                            break;
                        }
                    }

                    //we change the highscore because the player may have change it
                    m_lHighScore = m_LevelRenderer->GetHighScore();
                }

            }
        }

        DrawMenu();
    }
}
